/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.Bookmark;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;

public abstract class BookmarkAdapter<T extends Bookmark> extends ListViewAdapter<T> {
    
    public BookmarkAdapter(Context context, int viewResourceId) {
        super(context, viewResourceId);
    }

    @Override
    public View doGetView(final int position, View view, final ViewGroup parent) {
        CheckBox checkBox = findViewById(view, R.id.checkbox);
        checkBox.setOnCheckedChangeListener(null);
        checkBox.setChecked(((ListView) parent).isItemChecked(position));
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ((ListView) parent).setItemChecked(position, isChecked);
            }
        });

        return view;
    }
}
